package com.example.demo.controller;


import com.example.demo.dataobject.Food;
import com.example.demo.service.FoodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/food")
@Slf4j
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")  //解决跨域请求,但是无法做到对于session的共享，无法针对ajax请求，所以需要对参数值进行设置
public class FoodController {
    @Autowired
    private FoodService foodService;

    @RequestMapping("/listFood")
    public List<Food> listFood(@RequestParam("foodType") Integer foodType) {
        List<Food> result = new ArrayList<>();
        result = foodService.findByFoodTypeOrderByCarbonWater(foodType);
        return result;
    }

    @RequestMapping("/listFoodDesc")
    public List<Food> listFoodDesc(@RequestParam("foodType") Integer foodType) {
        List<Food> result = new ArrayList<>();
        result = foodService.findByFoodTypeOrderByCarbonWaterDesc(foodType);
        return result;
    }

    @RequestMapping("/searchFood")
    public List<Food> searchFoodByFoodName(@RequestParam("foodName") String foodName) {
        List<Food> result = foodService.findByFoodNameLikeOrderByCarbonWater("%" + foodName + "%");
        return result;
    }

}
